package com.dongdong.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdong.data.DataResult;
import com.dongdong.data.ResponseStatusEnum;
import com.dongdong.pojo.TbItemCat;
import com.dongdong.pojo.TbTypeTemplate;
import com.dongdong.service.ItemCatService;
import com.dongdong.service.TemplateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Deacription TODO
 * @Author Lenovo
 * @Date 2020/12/11 15:51
 * @Version 1.0
 **/
@Controller
@RequestMapping("itemCatController")
public class ItemCatController {

    private static final Logger logger = LoggerFactory.getLogger(ItemCatController.class);

    @Reference
    private ItemCatService itemCatService;

    @Reference
    private TemplateService templateService;

    @RequestMapping("list")
    public String list(Model model){
        List<TbTypeTemplate> tbTypeTemplates = templateService.getTemplate();
        model.addAttribute("temp",tbTypeTemplates);
        return "admin/item_cat";
    }

    @RequestMapping("selectItemCat")
    @ResponseBody
    public DataResult selectItemCat(Integer parentId){
        try {
            List<TbItemCat> list = itemCatService.selectItemCat(parentId);
            return DataResult.response(ResponseStatusEnum.SUCCESS).setData(list);
        }catch (Exception e){
            logger.error("查询失败");
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("getItemCatById/{id}")
    @ResponseBody
    public DataResult getItemCatById(@PathVariable("id") Long id){
        try {
//            if (id == 0){
//                return DataResult.response(ResponseStatusEnum.SUCCESS).setData(1);
//            }else {
                TbItemCat tbItemCat = itemCatService.getItemCatById(id);
                return DataResult.response(ResponseStatusEnum.SUCCESS).setData(tbItemCat);
           //}
        }catch (Exception e){
            logger.error("查询失败");
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("insertItemCat")
    @ResponseBody
    public DataResult insertItemCat(TbItemCat tbItemCat){
        try {
            int i = itemCatService.insertItemCat(tbItemCat);
            return DataResult.response(ResponseStatusEnum.SUCCESS).setData(i);
        }catch (Exception e){
            logger.error("增加错误");
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("getItemCat/{id}")
    @ResponseBody
    public DataResult getItemCat(@PathVariable("id") Long id){
        try {
            TbItemCat tbItemCat = itemCatService.getItemCatById(id);
            System.out.println(tbItemCat);
            return DataResult.response(ResponseStatusEnum.SUCCESS).setData(tbItemCat);
        }catch (Exception e){
            logger.error("查询失败");
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("updateInfo")
    @ResponseBody
    public DataResult updateInfo(TbItemCat tbItemCat){
        try {
            int i = itemCatService.updateInfo(tbItemCat);
            return DataResult.response(ResponseStatusEnum.SUCCESS).setData(i);
        }catch (Exception e){
            logger.error("增加错误");
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("deleteBatch")
    @ResponseBody
    public DataResult deleteBatch(@RequestParam("ids[]") Long[] ids){
        try {
            int i = itemCatService.deleteBatch(ids);
            return DataResult.response(ResponseStatusEnum.SUCCESS).setData(i);
        }catch (Exception e){
            logger.error("增加错误");
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

}

